home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / DragManSim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-28  |  8.5 KB  |  327 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10. #ifndef __DRAG__
  11. #include <Drag.h>
  12. #endif
  13.  
  14. #ifndef __FSPCOMPAT__
  15. #include <FSpCompat.h>
  16. #endif
  17.  
  18. #include    "CursorBalloon.h"
  19. #include    "FabWList.h"
  20. #include    "FabWmemman.h"
  21. #include    "FabTaskManager.h"
  22.  
  23. #include    "Globals.h"
  24. #include    "Assembler.h"
  25. #include    "DragManSim.h"
  26. #include    "myMemory.h"
  27.  
  28.  
  29. struct deferFileAction {
  30.     OSType    theType;
  31.     FSSpec    theFS;
  32.     };
  33.  
  34.  
  35. typedef struct myglobptr {
  36.     Boolean    canAcceptDrag;
  37.     Boolean    inContent;
  38.     } GlobalsData, *GlobalsPtr;
  39.  
  40. GlobalsData        gmyGlobals;
  41.  
  42. static pascal OSErr MyTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
  43.                         void *handlerRefCon, DragReference theDrag);
  44. static Boolean IsMyTypeAvailable(WindowPtr theWindow, DragReference theDrag);
  45. static pascal OSErr MyReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
  46.                         DragReference theDrag);
  47.  
  48. static void deferredFileAction(void *param);
  49.  
  50. /*
  51. OSErr MyInitDragManager(void)
  52. {
  53. OSErr    err;
  54.  
  55. err = InstallTrackingHandler(MyDefaultTrackingHandler, 0L, &myGlobals);
  56. if (err == noErr)
  57.     err = InstallReceiveHandler(MyDefaultReceiveHandler, 0L, &myGlobals);
  58.  
  59. return err;
  60. }
  61. */
  62.  
  63. OSErr MyInstallHWindow(WindowPtr theWindow)
  64. {
  65. FabWindowPtr    thefabw;
  66. OSErr       err;
  67.  
  68. if (thefabw = GetFabWindowPtr(theWindow)) {
  69.     thefabw->trackUPP = NewDragTrackingHandlerProc(MyTrackingHandler);
  70.     thefabw->recUPP = NewDragReceiveHandlerProc(MyReceiveHandler);
  71.     
  72.     if ((err = InstallTrackingHandler(thefabw->trackUPP, theWindow, &gmyGlobals)) == noErr) {
  73.         err = InstallReceiveHandler(thefabw->recUPP, theWindow, &gmyGlobals);
  74.         if (err)
  75.             RemoveTrackingHandler(thefabw->trackUPP, theWindow);
  76.         }
  77. //    else
  78. //        DebugStr("\pMyInstallHWindow error on InstallTrackingHandler");
  79.     }
  80. return err;
  81. }
  82.  
  83. void MyRemoveHWindow(WindowPtr theWindow)
  84. {
  85. FabWindowPtr    thefabw;
  86.  
  87. if (thefabw = GetFabWindowPtr(theWindow)) {
  88.     (void) RemoveTrackingHandler(thefabw->trackUPP, theWindow);
  89.     (void) RemoveReceiveHandler(thefabw->recUPP, theWindow);
  90.     if (thefabw->trackUPP) {
  91.         DisposeRoutineDescriptor(thefabw->trackUPP);
  92.         thefabw->trackUPP = nil;
  93.         }
  94.     if (thefabw->recUPP) {
  95.         DisposeRoutineDescriptor(thefabw->recUPP);
  96.         thefabw->recUPP = nil;
  97.         }
  98.     }
  99. }
  100.  
  101. pascal OSErr MyTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
  102.                         void *handlerRefCon, DragReference theDrag)
  103. {
  104. Rect    tempRect = {0, 0, 0, 0};
  105. GlobalsPtr        myGlobals = (GlobalsPtr) handlerRefCon;
  106. Point            mouse, localMouse;
  107. void (*theProc)(WindowPtr, RectPtr);
  108. DragAttributes    attributes;
  109. RgnHandle        hiliteRgn;
  110. FabWindowPtr    thefabw;
  111. OSErr    err;
  112.  
  113. if ((err = GetDragAttributes(theDrag, &attributes)) == noErr) {
  114.     switch(theMessage) {
  115. //        case dragTrackingEnterHandler:
  116. //            break;
  117.         case dragTrackingEnterWindow:
  118.             myGlobals->canAcceptDrag = IsMyTypeAvailable(theWindow, theDrag);
  119.             myGlobals->inContent = false;
  120.             break;
  121.         case dragTrackingInWindow:
  122.             if (myGlobals->canAcceptDrag) {
  123.                 if ((err = GetDragMouse(theDrag, &mouse, 0L)) == noErr) {
  124.                     localMouse = mouse;
  125.                     GlobalToLocal(&localMouse);
  126.                     if (attributes & dragHasLeftSenderWindow) {
  127.                         if (thefabw = GetFabWindowPtr(theWindow)) {
  128.                             theProc = thefabw->getDragHiliteRectProc;
  129.                             if (theProc)
  130.                                 theProc(theWindow, &tempRect);
  131.                             }
  132.                         if (PtInRect(localMouse, &tempRect)) {
  133.                             if (myGlobals->inContent == false) {
  134.                                 hiliteRgn = NewRgn();
  135.                                 if (hiliteRgn) {
  136.                                     //InsetRect(&tempRect, 1, 1);
  137.                                     RectRgn(hiliteRgn, &tempRect);
  138.                                     (void) ShowDragHilite(theDrag, hiliteRgn, true);
  139.                                     DisposeRgn(hiliteRgn);
  140.                                     }
  141.                                 myGlobals->inContent = true;
  142.                                 }
  143.                             }
  144.                         else {
  145.                             if (myGlobals->inContent) {
  146.                                 (void) HideDragHilite(theDrag);
  147.                                 myGlobals->inContent = false;
  148.                                 }
  149.                             }
  150.                         }
  151.                     }
  152.     //            MyTrackItemUnderMouse(localMouse, theWindow);
  153.                 }
  154.             break;
  155.         case dragTrackingLeaveWindow:
  156.             if (myGlobals->canAcceptDrag && myGlobals->inContent) {
  157.                 (void) HideDragHilite(theDrag);
  158.                 }
  159.             myGlobals->canAcceptDrag = false;
  160.             break;
  161. //        case dragTrackingLeaveHandler:
  162. //            break;
  163.         }
  164.     }
  165. return err;
  166. }
  167.  
  168. Boolean IsMyTypeAvailable(WindowRef theWindow, DragReference theDrag)
  169. {
  170. HFSFlavor        hfsData;
  171. PromiseHFSFlavor    phfsData;
  172. //HVolumeParam    mypb;
  173. FSSpec            copySpec;
  174. //HFSFlavor        *theData;
  175. //AliasHandle        tempFolder;
  176. Size            dataSize;
  177. //FlavorFlags        theFlags;
  178. ItemReference    theItem;
  179. unsigned short    items, index;
  180. OSErr            err;
  181. Boolean            targetFolder, isAnAlias;
  182. Boolean            retVal = true;
  183.  
  184.  
  185. if ((err = CountDragItems(theDrag, &items)) == noErr) {
  186.     for (index = 1; (index <= items) && retVal; index++) {
  187.         if ((err = GetDragItemReferenceNumber(theDrag, index, &theItem)) == noErr) {
  188.             dataSize = sizeof(PromiseHFSFlavor);
  189.             if (err = GetFlavorData(theDrag, theItem, flavorTypePromiseHFS, &phfsData, &dataSize, 0L)) {
  190.                 dataSize = sizeof(HFSFlavor);
  191.                 if (noErr == (err = GetFlavorData(theDrag, theItem, flavorTypeHFS, &hfsData, &dataSize, 0L))) {
  192.                     switch (hfsData.fileType) {
  193.                         case kFTY_REG:
  194.                             retVal = theWindow == gWPtr_Registers;
  195.                             break;
  196.                         case kFTY_RAM:
  197.                         case 'TEXT':
  198.                             retVal = theWindow == gWPtr_Disasm || theWindow == gWPtr_Dump;
  199.                             break;
  200.                         default:
  201.                             retVal = false;
  202.                         }
  203.                     if (retVal) {
  204.                         copySpec = hfsData.fileSpec;
  205.                         if (err = ResolveAliasFile(©Spec, true, &targetFolder, &isAnAlias))
  206.                             retVal = false;
  207.                         }
  208.                     }
  209.                 else
  210.                     retVal = false;
  211.                 }
  212.             }
  213.         else
  214.             retVal = false;
  215.         }
  216.     }
  217. else
  218.     retVal = false;
  219. return retVal;
  220. }
  221.  
  222. pascal OSErr MyReceiveHandler(WindowPtr /*theWindow*/, void *handlerRefCon,
  223.                         DragReference theDrag)
  224. {
  225. GlobalsPtr        myGlobals = (GlobalsPtr) handlerRefCon;
  226. //HVolumeParam    mypb;
  227. HFSFlavor        hfsData;
  228. PromiseHFSFlavor    phfsData;
  229. FInfo    myInfo;
  230. FSSpec            copySpec;
  231. //AliasHandle        tempFolder;
  232. //Point            mouse;
  233. ItemReference    theItem;
  234. //FlavorFlags        theFlags;
  235. Size            dataSize;
  236. struct deferFileAction    *param;
  237. void    *theTask;
  238. unsigned short    items, index;
  239. OSErr            err = noErr;
  240. Boolean            targetFolder, isAnAlias;
  241.  
  242. //if ((err = GetDragMouse(theDrag, &mouse, 0L)) == noErr) 
  243. if (myGlobals->canAcceptDrag && myGlobals->inContent) {
  244.     if ((err = CountDragItems(theDrag, &items)) == noErr) {
  245.         for (index = 1; (index <= items) && (err == noErr); index++) {
  246.             if ((err = GetDragItemReferenceNumber(theDrag, index, &theItem)) == noErr) {
  247. //                if ((err = GetFlavorFlags(theDrag, theItem, flavorTypeHFS, &theFlags)) == noErr) 
  248.                 dataSize = sizeof(PromiseHFSFlavor);
  249.                 if ((err = GetFlavorData(theDrag, theItem, flavorTypePromiseHFS, &phfsData, &dataSize, 0L)) == noErr) {
  250.                     dataSize = sizeof(FSSpec);
  251.                     hfsData.fileType = kContainerAliasType;
  252.                     err = GetFlavorData(theDrag, theItem, phfsData.promisedFlavor, &hfsData.fileSpec, &dataSize, 0L);
  253.                     }
  254.                 else {
  255.                     dataSize = sizeof(HFSFlavor);
  256.                     err = GetFlavorData(theDrag, theItem, flavorTypeHFS, &hfsData, &dataSize, 0L);
  257.                     }
  258.                 if (err == noErr) {
  259.                     copySpec = hfsData.fileSpec;
  260.                     if ((err = ResolveAliasFile(©Spec, true, &targetFolder, &isAnAlias)) == noErr) {
  261.                         if (targetFolder == false) {
  262.                             if ((err = FSpGetFInfoCompat(©Spec, &myInfo)) == noErr) {
  263.                                 switch (myInfo.fdType) {
  264.                                     case kFTY_REG:
  265.                                     case kFTY_RAM:
  266.                                     case 'TEXT':
  267.                                         param = fmalloc(sizeof(struct deferFileAction));
  268.                                         if (param) {
  269.                                             param->theType = myInfo.fdType;
  270.                                             param->theFS = copySpec;
  271.                                             theTask = Fab_CreateSystemTask(deferredFileAction, param, true);
  272.                                             if (theTask) {
  273.                                                 Fab_ScheduleSystemTask(theTask);
  274.                                                 }
  275.                                             else {
  276.                                                 err = dragNotAcceptedErr;
  277.                                                 ffree(param);
  278.                                                 }
  279.                                             }
  280.                                         else
  281.                                             err = dragNotAcceptedErr;
  282.                                         break;
  283.                                     default:
  284.                                         err = dragNotAcceptedErr;
  285.                                     }
  286.                                 }
  287.                             }
  288.                         else
  289.                             err = dragNotAcceptedErr;
  290.                         }
  291.                     }
  292. //                    
  293.                 }
  294.             }
  295.         }
  296.     }
  297. else
  298.     err = dragNotAcceptedErr;
  299.  
  300. if (err)
  301.     err = dragNotAcceptedErr;
  302. return err;
  303. }
  304.  
  305. void deferredFileAction(void *param)
  306. {
  307. struct deferFileAction *myData = param;
  308. OSErr    err;
  309.  
  310. switch (myData->theType) {
  311.     case kFTY_REG:
  312.         err = OpenProcessorState(&myData->theFS);
  313.         UnloadSeg(OpenProcessorState);
  314.         break;
  315.     case kFTY_RAM:
  316.         err = myOpenFile(&myData->theFS, gMMemory, kSIZE_RAM);
  317.         UnloadSeg(myOpenFile);
  318.         break;
  319.     case 'TEXT':
  320.         err = myAsmFile(&myData->theFS);
  321.         UnloadSeg(myAsmFile);
  322.         break;
  323.     }
  324. ffree(myData);
  325. }
  326.  
  327.